package com.xiaoge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoge.entity.WebsiteLink;
import com.xiaoge.exception.BusinessException;
import com.xiaoge.service.WebsiteLinkService;
import com.xiaoge.mapper.WebsiteLinkMapper;
import com.xiaoge.vo.WebsiteLinkVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
* @author 
* @description 针对表【edu_website_link(站点友情链接)】的数据库操作Service实现
* @createDate 2022-10-28 09:36:15
*/
@Service
public class WebsiteLinkServiceImpl extends ServiceImpl<WebsiteLinkMapper, WebsiteLink>
    implements WebsiteLinkService {

    @Override
    public Page<WebsiteLink> getWebsiteLinkList(Page<WebsiteLink> page, WebsiteLinkVO websiteLinkVO) {

//        LambdaQueryWrapper<WebsiteLink> wrapper = new LambdaQueryWrapper<>();
//
//        wrapper.like(WebsiteLink::getLinkName,websiteLinkVO.getLinkName());

//        List<WebsiteLink> websiteLinkList = this.baseMapper.selectList(wrapper);

        List<WebsiteLink> websiteLinkList = this.baseMapper.getWebsiteLinkList(page, websiteLinkVO);

        //将刚刚动态sql查询的内容给page对象,让它去实现分页
        page.setRecords(websiteLinkList);

        return page;
    }

    @Override
    public void deleteById(Long id) {

        this.baseMapper.deleteById(id);

    }

    @Override
    public WebsiteLink selectById(Long id) {
        WebsiteLink websiteLink = this.baseMapper.selectById(id);
        if(websiteLink==null){
            throw new BusinessException("要编辑的链接信息不存在");
        }
        return websiteLink;
    }

    @Override
    public void updateWebsiteLink(WebsiteLink websiteLink) {
        Optional<WebsiteLink> op = Optional.ofNullable(websiteLink);

        op.ifPresent(o->{
            this.baseMapper.updateById(websiteLink);
        });
    }

    @Override
    public void addWebsiteLink(WebsiteLink websiteLink) {
        Optional<WebsiteLink> op = Optional.ofNullable(websiteLink);

        op.ifPresent(o->{
            this.baseMapper.insert(websiteLink);
        });
    }
}




